package net.googlecode.demenkov.task5.calculator;

import net.googlecode.demenkov.task5.model.Manager;

import java.math.BigDecimal;

/**
 * Class for calculating salary for Manager
 */
public class ManagerSalaryCalculator implements Calculator {
    private static final BigDecimal HOURS_PER_MONTH = new BigDecimal(170);
    private BigDecimal tax;

    public BigDecimal calculateSalary(Manager person) {
        BigDecimal salary;

        if (person.isPerHour()) {
            salary = calculatePerHourRate(person);
        } else {
            salary = calculateMonthRate(person);
        }

        if (person.hasBonus()) {
            salary = salary.add(person.getBonusAmount());
        }

        if (tax != null) {
            salary = salary.subtract(tax);
        }
        return salary;
    }

    /**
     * Calculate non-per hour rate
     *
     * @param person the manager
     * @return calculated rate
     */
    public BigDecimal calculateMonthRate(Manager person) {
        return person.getRate() != null ? person.getRate() : BigDecimal.ZERO;
    }

    /**
     * Calculate per hour rate
     *
     * @param person the manager
     * @return calculated rate
     */
    private BigDecimal calculatePerHourRate(Manager person) {
        return person.getRate() != null ? person.getRate().multiply(HOURS_PER_MONTH) : BigDecimal.ZERO;
    }

    public void setTax(BigDecimal tax) {
        this.tax = tax;
    }
}
